﻿using System;
using System.Collections.Generic;

namespace Librarinth
{
	public struct BufferArray<T> : IEnumerable<T>
	{
		public T[] Datas { get { return datas; } }
		public int Count { get { return count; } }

		T[] datas;
		int count;
		public void Clear() { count	= 0; }
		public void Add(T data) { Add(ref data); }
		public void Add(ref T data)
		{
			if(Datas == null || datas.Length <= count)
			{
				int newSize	= datas == null ? 4 : datas.Length * 2;
				Array.Resize(ref datas,newSize);
			}
			datas[count]	= data;
			count++;
		}

		#region IEnumerable<T> Members

		IEnumerator<T> IEnumerable<T>.GetEnumerator()
		{
			int i	= 0;
			while(i < count)
				yield return datas[i++];
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{ return (this as IEnumerable<T>).GetEnumerator(); }

		#endregion
	}
}
